package geekstudy.week5;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 * https://leetcode-cn.com/problems/online-election/submissions/
 * 911. 在线选举
 *
 * 执行结果：  通过  显示详情
 *
 * 添加备注
 * 执行用时：99 ms, 在所有 Java 提交中击败了45.35% 的用户
 * 内存消耗：47.6 MB, 在所有 Java 提交中击败了78.49% 的用户
 *
 */
public class TopVotedCandidate {

    //Map<Integer, Map<Integer, Integer>> timePersonVoteMap = new LinkedHashMap<Integer, Map<Integer, Integer>>();
    TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();

    public TopVotedCandidate(int[] persons, int[] times) {
        int currentLead = times[0];
        int maxVote = 0;
        // int[] voteNumber = new int[times.length];
        Map<Integer, Integer> personVoteMap = new LinkedHashMap<Integer, Integer>();
        for(int size = 0; size < times.length; size++) {
            int currentTime = times[size];
            int currentPerson = persons[size];

            int personVote = 1;
            if(personVoteMap.containsKey(currentPerson)) {
                personVote = personVoteMap.get(currentPerson) + 1;
                personVoteMap.put(currentPerson, personVote);
            } else {
                personVoteMap.put(currentPerson, personVote);
            }
            if(maxVote <= personVote) {
                maxVote = personVote;
                currentLead = currentPerson;
            }
            map.put(currentTime, currentLead);

            //if(!timePersonVoteMap.containsKey(currentTime)) {
            //    timePersonVoteMap.put(currentTime, new LinkedHashMap<Integer, Integer>());
            //}
            //Map<Integer, Integer> personMap = timePersonVoteMap.get(currentTime);
            //if(personMap.containsKey(currentPerson)) {
            //    personMap.put(currentPerson, personMap.get(currentPerson) + 1);
            //} else {
            //    personMap.put(currentPerson, 1);
            //}
        }
    }

    public int q(int t) {
        int q = map.floorEntry(t).getValue();
        return q;
    }
}

/**
 * Your TopVotedCandidate object will be instantiated and called as such:
 * TopVotedCandidate obj = new TopVotedCandidate(persons, times);
 * int param_1 = obj.q(t);
 */

